package com.yc.spring.mvc.util;

import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.StandardReflectionParameterNameDiscoverer;

import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.core.*;

public class Utils {
    private static DefaultParameterNameDisconverer dpnd = new DefaultParameterNameDisconverer();
    // 方式一
    // 返回方法的参数名列表
    // 参考: https://www.jb51.net/article/232059.htm
    public static String[] getParameterNames(Method method){
        return dpnd.getParameterNames(method);
    }
    // 方式二
    public static void main(String[] args) throws NoSuchMethodException {
        // 反射方式
        StandardReflectionParameterNameDiscoverer nd = new StandardReflectionParameterNameDiscoverer();
        // 本地变量表
        LocalVariableTableParameterNameDiscoverer lv = new LocalVariableTableParameterNameDiscoverer();
        // 获取方法对象
        Method method = Utils.class.getDeclaredMethod("Name", String.class);
        // 参数列表方式 1
        System.out.println(Arrays.toString(nd.getParameterNames(method)));
        // 参数列表方式 2
        System.out.println(Arrays.toString(lv.getParameterNames(method)));
    }
}
class DefaultParameterNameDisconverer extends PrioritizedParameterNameDiscoverer{
    public DefaultParameterNameDisconverer() {
        if(KotlinDetector.isKotlinReflectPresent()){
            addDiscoverer(new KotlinReflectionParameterNameDiscoverer());
        }
        addDiscoverer(new StandardReflectionParameterNameDiscoverer());
        addDiscoverer(new LocalVariableTableParameterNameDiscoverer());
    }
}